import React from 'react'
import {
  Form,
  Input,
  Button,
} from 'antd-mobile'
import http from '../../utils/http'
import { useNavigate } from 'react-router-dom'
function Index() {
  let navigate = useNavigate()
  const onFinish = async (value) => {
    let res = await http.post('/api/login', {
      username: value.username,
      password: value.password
    })

    console.log('登录', res);

    let { code, data } = res.data

    if (code === 200) {
      localStorage.setItem('token',data.token)
      localStorage.setItem('userInfo',JSON.stringify(data.userInfo))
      navigate('/')
    }
  }
  return (
    <div>
      <Form
        layout='horizontal'
        onFinish={onFinish}
        footer={
          <Button block type='submit' color='primary' size='large'>
            提交
          </Button>
        }
      >
        <Form.Item
          name='username'
          label='用户名'
          rules={[{ required: true, message: '用户名不能为空' }]}
        >
          <Input onChange={console.log} placeholder='请输入用户名' />
        </Form.Item>
        <Form.Item
          name='password'
          label='密码'
          rules={[{ required: true, message: '密码不能为空' }]}
        >
          <Input onChange={console.log} placeholder='请输入密码' />
        </Form.Item>
      </Form>

    </div>
  )
}

export default Index
